package dictionaryTokenScanner;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import main.tokens;

public class TokenScanner 
{
	private ArrayList<String> arlDictionary;
	
	public TokenScanner(String pDicLoc)
	{
		// get file list where the path has   
		File file = new File(pDicLoc);   
		// get the folder list   
		File[] dicArray = file.listFiles(); 
		
		arlDictionary = new ArrayList<String>();
		
		for (int i = 0; i < dicArray.length; i++)
		{
			getCntDic(dicArray[i]);
		}
		
		Collections.sort(arlDictionary);
		
	}
	
	private void getCntDic(File pCntDic)
	{
		try
		{
			FileReader reader = new FileReader(pCntDic);
			BufferedReader br = new BufferedReader(reader);
			String strCntLine = null;
			
			while((strCntLine = br.readLine()) != null) 
			{
				String s = "\\w+";
		        Pattern  pattern=Pattern.compile(s);  
		        Matcher  ma=pattern.matcher(strCntLine);  
		        String strTmp;
		        while(ma.find())
		        {
		        	strTmp = ma.group();
		        	//System.out.println(strTmp);
		        	//if (!arlDictionary.contains(strTmp))
		        	//{
		        		arlDictionary.add(strTmp); 
		        	//}
		        }  
			}
			
			br.close();
			reader.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public ArrayList<tokens> tokenScanner(ArrayList<tokens> pTokensArray)
	{
		ArrayList<tokens> arlTokensArray = pTokensArray;
		int intCount = 0;
		
		for (int i = 0; i < arlTokensArray.size(); i++)
		{
			if (arlDictionary.contains(arlTokensArray.get(i).getToken().toLowerCase()))
			{
				arlTokensArray.get(i).setState(1);
				intCount ++;
			}
		}
		System.out.println(intCount);
		return arlTokensArray;
	}
}
